package com.hdyg.sixg.activity.home;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.allen.library.SuperTextView;
import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.adapter.CashBiAdapter;
import com.hdyg.sixg.entry.CashBiBean;
import com.hdyg.sixg.mvp.Contrant.CCashBi;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.presenter.PCashBi;
import com.hdyg.sixg.util.PopWindowUtil;
import com.hdyg.sixg.util.StringUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import java.util.List;
import butterknife.BindView;

/**
 * @author CZB
 * @describe 提币界面
 * @time 2019/4/8 10:31
 */
public class CashBiActivity extends BaseActivity implements CCashBi.IVCashBi {


    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.sv_type)
    SuperTextView svType;
    @BindView(R.id.et_address)
    EditText etAddress;
    @BindView(R.id.et_number)
    EditText etNumber;
    @BindView(R.id.sv_fee)
    SuperTextView svFee;
    @BindView(R.id.sv_all)
    SuperTextView svAll;
    @BindView(R.id.et_pay_pwd)
    EditText etPayPwd;
    @BindView(R.id.bt_sure)
    Button btSure;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private String type, amount, trapass, walletaddress, fee;
    private CCashBi.IPCashBi mPresenter;
    private List<CashBiBean.ListBean> coinBeans;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_cash_bi;
    }

    @Override
    protected void initView() {
        onClickListener();
        tvTopTitle.setText(R.string.main_cash_bi);
    }

    private void onClickListener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            initData();
        });
        llTopLeft.setOnClickListener(v -> finish());
        //类型
        svType.setOnSuperTextViewClickListener(superTextView -> {
            showCoinTypePop(superTextView);
        });
        //数量监听
        etNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                amount = s.toString();
                if (!TextUtils.isEmpty(amount)) {
                    double allMoney = (1 + Double.valueOf(fee) / 100 ) * Double.valueOf(amount);
                    svAll.setRightString(StringUtil.dealFloatStr(allMoney));
                } else {
                    svAll.setRightString("0");
                }
            }
        });
        //确认提现
        btSure.setOnClickListener(v -> {
            amount = etNumber.getText().toString().trim();
            trapass = etPayPwd.getText().toString().trim();
            walletaddress = etAddress.getText().toString().trim();
            if (TextUtils.isEmpty(type)) {
                toastNotifyShort(R.string.phone_switch);
                return;
            }
            if (TextUtils.isEmpty(walletaddress)) {
                toastNotifyShort(R.string.cash_bi_address_hint);
                return;
            }
            if (TextUtils.isEmpty(amount)) {
                toastNotifyShort(R.string.cash_bi_number_hint);
                return;
            }
            if (TextUtils.isEmpty(trapass)) {
                toastNotifyShort(R.string.pay_pwd_hint);
                return;
            }
            mPresenter.pGetApplyCashBi(BaseUrl.CASHI_BI_APPLY_URL, GetParamUtil.getCashBiApplyParam(type, amount, trapass, walletaddress));
        });
    }

    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.CASHI_BI_INIT_URL, GetParamUtil.getCashBiInitParam());
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PCashBi(this);
    }

    private void showCoinTypePop(View v) {
        if (coinBeans == null) {
            toastNotifyShort(R.string.toast_coin);
            return;
        }
        View view = View.inflate(mContext, R.layout.pop_cash_bi_coin, null);
        RecyclerView rvArea = view.findViewById(R.id.rv_coin);
        rvArea.setLayoutManager(new LinearLayoutManager(mContext));
        CashBiAdapter areaAdapter = new CashBiAdapter(R.layout.item_cash_bi_coin, coinBeans);
        rvArea.setAdapter(areaAdapter);
        areaAdapter.setOnItemClickListener((adapter, view1, position) -> {
            type = coinBeans.get(position).getType();
            svType.setRightString(coinBeans.get(position).getCoin());
            svType.setRightTextColor(getResources().getColor(R.color.white));
            mPopupWindow.dismiss();
        });
        mPopupWindow = PopWindowUtil.getInstance().makePopupWindow(mContext, view, ViewGroup.LayoutParams.MATCH_PARENT)
                .showAsDropDown(mContext, v, 0);

    }

    @Override
    public void vGetInitSuccess(CashBiBean dataBean) {
        fee = dataBean.getRate();
        String feeStr = StringUtil.getPercentage(Float.valueOf(fee),100);
        svFee.setRightString(feeStr);
        coinBeans = dataBean.getList();
    }

    @Override
    public void vGetApplyCashBiSuccess(String dataBean) {
        toastNotifyShort(dataBean);
        setResult(RESULT_OK);
        finish();
    }
}
